package com.ms.seguridad.support;


import com.ms.seguridad.bean.ModuleBean;
import org.primefaces.model.menu.DefaultMenuItem;
import org.primefaces.model.menu.DefaultMenuModel;
import org.primefaces.model.menu.DefaultSubMenu;
import org.primefaces.model.menu.MenuGroup;

import javax.faces.context.FacesContext;
import java.util.List;

public class WebHelper {

    public static DefaultMenuModel getToolBarMenu(FacesContext facesContext, List<ModuleBean> moduleList) {
        DefaultMenuModel menuModel = new DefaultMenuModel();
        DefaultSubMenu subMenuModel = null;
        MenuGroup htmlMenuGroup=null;
        for (ModuleBean moduleBean : moduleList) {
            subMenuModel = new DefaultSubMenu(moduleBean.getModuleName());
            DefaultMenuItem subMenuItem = null;

            for (ModuleBean moduleItemBean : moduleBean.getModuleList()) {
                if (moduleItemBean.getModuleList().isEmpty()) {
                    subMenuItem = new DefaultMenuItem(moduleItemBean.getModuleName());
                    subMenuItem.setUrl(moduleItemBean.getModuleUrl());
                    subMenuModel.addElement(subMenuItem);
                } else {
                    htmlMenuGroup = new DefaultSubMenu(moduleItemBean.getModuleName());
                    for (ModuleBean moduleSubItemBean : moduleItemBean.getModuleList()) {
                        subMenuItem = new DefaultMenuItem(moduleSubItemBean.getModuleName());
                        subMenuItem.setUrl(moduleItemBean.getModuleUrl());
                        htmlMenuGroup.getElements().add(subMenuItem);
                    }
                    subMenuModel.addElement(htmlMenuGroup);
                }
            }
            menuModel.addElement(subMenuModel);
        }
        return menuModel;
    }

}